package com.monitoring.management.controller;

/**
 * Created by liujunnan on 2017/5/31.
 */

import com.monitoring.management.common.File.UploadFileUtils;
import com.monitoring.management.common.Response;
import com.monitoring.management.common.constant.ConstSession;
import com.monitoring.management.common.constant.ConstURLString;
import com.monitoring.management.core.command.LoginCommand;
import com.monitoring.management.core.command.RegisterCommand;
import com.monitoring.management.core.command.query.QueryUserCommand;
import com.monitoring.management.core.dto.UserDTO;
import com.monitoring.management.facade.UserFacede;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Inject
    private UserFacede userFacade;

    /* @RequestMapping("/show")*/
   /* public String toIndex(HttpServletRequest request, Model model) {
        int userId = Integer.parseInt(request.getParameter("id"));
        UserDTO user = this.userService.getUserById(userId);
        model.addAttribute("user", user);
        return "show";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(UserDTO user) throws Exception {

        user = userService.checkLogin(user.getUserName(), user.getPassword());
        if (user != null) {
            return "success";
        }
        return "fail";
    }*/

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Response login(HttpSession session, @RequestBody LoginCommand loginCommand) throws Exception {

        Response response = userFacade.login(loginCommand);
        UserDTO userDTO = (UserDTO) response.getData();

        if (null == userDTO) {
            response.setCode(1);
            response.setData(null);
            return response;
        } else {
            //登陆成功之后将用户设置到session中
            session.setAttribute(ConstSession.LOGIN_USER_SESSION, userDTO);
            return response;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Response register(@RequestBody RegisterCommand registerCommand) {
        return userFacade.register(registerCommand);
    }

    @ResponseBody
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public Response show(@RequestBody QueryUserCommand queryUserCommand) {
        return userFacade.pageUser(queryUserCommand);
    }

    @ResponseBody
    @RequestMapping(value = "/outExcel.do", method = RequestMethod.GET)
    public Response getExcelBySupply(String supplierId, HttpServletResponse res) throws IOException {
        return userFacade.getExcelBySupply(supplierId, res);
    }

  @ResponseBody
    @RequestMapping(value = "/uploadFile.do", method = RequestMethod.POST)
        public Response userCheckin(@RequestBody String supplierID, HttpServletRequest request) {
        Response response = new Response();

        String filePath = "C:\\Users\\liujunnan\\Desktop\\respon111se.xls";
        try {
            response = userFacade.usersListSave(filePath, supplierID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    @ResponseBody
    @RequestMapping(value = "/uploadFile1.do", method = RequestMethod.POST)
    public Response userCheckin(HttpServletRequest request ) {
        Response response = new Response();

        String filePath = "";

        try {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            if (multipartResolver.isMultipart(request)) {
                List fileList = UploadFileUtils.uploadFile(0, request, ConstURLString.UPLOAD_GOODS_EXCEL_LOCAL_PATH);
                if (fileList != null && fileList.size() > 0) {
                    filePath = (String) ((Map) fileList.get(0)).get("localPath");
                }
            }
            response = userFacade.ListSave(filePath);
            userFacade.usersListSave(filePath,"1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //刪除本地文件
        UploadFileUtils.deleteFile(filePath);
        return response;

    }
    @ResponseBody
    @RequestMapping(value = "/outExcel1.do", method = RequestMethod.GET)
    public Response getExcel1(String supplierId, HttpServletResponse res) throws IOException {
        return userFacade.getExcelBySupply(supplierId, res);
    }

}
